package com.zoe.onelink.core.autoconfigure;

import cn.hutool.core.util.DesensitizedUtil;
import cn.hutool.core.util.StrUtil;
import com.zoe.onelink.core.web.jackson.sensitive.strategy.ISensitiveStrategy;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * <p>标题: </p>
 * <p>描述: </p>
 * <p>版权: Copyright (c) 2021</p>
 * <p>公司: 智业软件股份有限公司</p>
 *
 * @version: 1.0
 * @author: ruansheng
 * @date: 2021-11-11
 */
@Configuration(proxyBeanMethods = false)
public class OnelinkJacksonSensitiveAutoConfiguration {

    @Bean(ISensitiveStrategy.BEAN_NAME_PREFIX + "PHONE")
    public ISensitiveStrategy phoneSensitiveStrategy() {
        return (clazz, value, args) -> DesensitizedUtil.mobilePhone(value);
    }

    @Bean(ISensitiveStrategy.BEAN_NAME_PREFIX + "EMAIL")
    public ISensitiveStrategy emailSensitiveStrategy() {
        return (clazz, value, args) -> DesensitizedUtil.email(value);
    }

    @Bean(ISensitiveStrategy.BEAN_NAME_PREFIX + "ID_CARD")
    public ISensitiveStrategy idCardSensitiveStrategy() {
        return (clazz, value, args) -> DesensitizedUtil.idCardNum(value, 6, 4);
    }

    @Bean(ISensitiveStrategy.BEAN_NAME_PREFIX + "PASSWORD")
    public ISensitiveStrategy passwordSensitiveStrategy() {
        return (clazz, value, args) -> DesensitizedUtil.password(value);
    }

    @Bean(ISensitiveStrategy.BEAN_NAME_PREFIX + "CAR_NUM")
    public ISensitiveStrategy carNumSensitiveStrategy() {
        return (clazz, value, args) -> DesensitizedUtil.carLicense(value);
    }

    @Bean(ISensitiveStrategy.BEAN_NAME_PREFIX + "BANK_CARD")
    public ISensitiveStrategy bankCardSensitiveStrategy() {
        return (clazz, value, args) -> DesensitizedUtil.bankCard(value);
    }

    @Bean(ISensitiveStrategy.BEAN_NAME_PREFIX + "CHINESE_NAME")
    public ISensitiveStrategy chineseNameSensitiveStrategy() {
        return (clazz, value, args) -> DesensitizedUtil.chineseName(value);
    }

    @Bean(ISensitiveStrategy.BEAN_NAME_PREFIX + "ADDRESS")
    public ISensitiveStrategy addressSensitiveStrategy() {
        return (clazz, value, args) -> {
            if (StrUtil.isBlank(value)) {
                return "";
            }
            int len;
            if (args != null && args.length > 0) {
                len = Integer.parseInt(args[0]);
            } else {
                len = value.length() - value.length() / 2;
            }
            return DesensitizedUtil.address(value, len);
        };
    }

    @Bean(ISensitiveStrategy.BEAN_NAME_PREFIX + "CUSTOM")
    public ISensitiveStrategy customSensitiveStrategy() {
        return (clazz, value, args) -> {
            if (StrUtil.isBlank(value)) {
                return "";
            }
            int front, end;
            if (args == null || args.length == 0) {
                front = 0;
                end = 0;
            } else if (args.length == 1) {
                front = Integer.parseInt(args[0]);
                end = value.length();
            } else {
                front = Integer.parseInt(args[0]);
                end = value.length() - Integer.parseInt(args[1]);
            }
            return StrUtil.hide(value, front, end);
        };
    }

}
